package folder.main.exp0.controllers.heuristic.bandwidth;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import folder.main.exp0.controllers.heuristic.Heuristic;
import folder.model.network.Network;
import folder.model.node.Node;

public class GAB_H extends Heuristic
{
	public GAB_H( Network network )
	{
		super( network, null, -1 );
	}
	
	public GAB_H( Network network, double[][] delays, int T )
	{
		super( network, delays, T );
		
		this.setMatches( this.minimizing( this.matching( this.arranging() ) ) );
	}
	
	protected List< Node > arranging()
	{
		double ABW[] = ABWsTable( network.getNodes(), false );
		
		LinkedHashMap< Node, Double > pairs = new LinkedHashMap< Node, Double >();
		
		for( Node node: network.getNodes() )
			pairs.put( node, ABW[ node.getIdentifier() ] );
		
		LinkedHashMap< Node, Double > arrangedPairs = new LinkedHashMap< Node, Double >();
		
		while( !pairs.isEmpty() )
		{
			Iterator< Entry< Node, Double > > iterator = pairs.entrySet().iterator();
			double maxABW = -1;
			Node maxABWnode = null;
									
			while( iterator.hasNext() )
			{
				Entry< Node, Double > entry = iterator.next();
				
				if( entry.getValue() > maxABW )
				{
					maxABWnode = entry.getKey();
					maxABW = entry.getValue();
				}
			}
			
			arrangedPairs.put( maxABWnode, maxABW );
			pairs.remove( maxABWnode );
		}
		
		List< Node > list = new ArrayList< Node >();
		
		if( arrangedPairs.entrySet().iterator().next().getValue() != 0 )
			list.addAll( arrangedPairs.keySet() );
		else
			list = null;
		
		return list;
	}

}
